package com.jellyfishumbrella;

import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import com.jellyfishumbrella.GL.RendererGL;

import static org.lwjgl.opengl.GL11.*;

public class TikisAndTotemsMain{
	private static boolean		do3D		= true;
	private static final String	LOOKANDFEEL	= "System";

	GameLogic					gameLogic;
	GamePanel					gamePanel;
	JTextField					txtDebug;

	RendererGL displayGL;
	long						lastFrame;
	int							fps;
	long						lastFPS;

	public static void main(String[] args) {
		setupUISkin();

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TikisAndTotemsMain frame = new TikisAndTotemsMain();
					//frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void setupUISkin() {
		String lookAndFeel = null;

		if (LOOKANDFEEL != null) {
			if (LOOKANDFEEL.equals("Metal")) {
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
				// an alternative way to set the Metal L&F is to replace the
				// previous line with:
				// lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";

			}

			else if (LOOKANDFEEL.equals("System")) {
				lookAndFeel = UIManager.getSystemLookAndFeelClassName();
			}

			else if (LOOKANDFEEL.equals("Motif")) {
				lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			}

			else if (LOOKANDFEEL.equals("GTK")) {
				lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
			}

			else {
				System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL);
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
			}

			try {

				UIManager.setLookAndFeel(lookAndFeel);

			}

			catch (ClassNotFoundException e) {
				System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel);
				System.err.println("Did you include the L&F library in the class path?");
				System.err.println("Using the default look and feel.");
			}

			catch (UnsupportedLookAndFeelException e) {
				System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform.");
				System.err.println("Using the default look and feel.");
			}

			catch (Exception e) {
				System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason.");
				System.err.println("Using the default look and feel.");
				e.printStackTrace();
			}
		}
	}

	/**
	 * Create the frame.
	 */
	public TikisAndTotemsMain() {
		if (do3D) {
			createGUI3D();
		} else {
			createGUI2D();
		}
		gameLogic = new GameLogic();
		if (do3D) {
			try {
				displayGL.connectToLogic(gameLogic);
			} catch (LWJGLException e) {
				e.printStackTrace();
			}
		} else {
			gamePanel.landscapeBuffer.data = gameLogic.island;
		}
		gameLogic.gameLoop();
	}

	public void createGUI2D() {
		JFrame mainWindow = new JFrame();
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainWindow.setBounds(100, 100, 850, 850);
		mainWindow.getContentPane().setLayout(null);

		mainWindow.setTitle("Totem - pre-alpha");
		mainWindow.setDefaultCloseOperation(mainWindow.EXIT_ON_CLOSE);
		mainWindow.setResizable(false);

		gamePanel = new GamePanel();
		gamePanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		gamePanel.setBounds(10, 10, 600, 600);
		mainWindow.getContentPane().add(gamePanel);
		gamePanel.setLayout(null);

		txtDebug = new JTextField();
		txtDebug.setBounds(10, 620, 600, 20);
		mainWindow.getContentPane().add(txtDebug);

		gamePanel.addMouseMotionListener(new MouseOverListener());
		gamePanel.addMouseWheelListener(new MouseZoomListener());
	}

	public void createGUI3D() {
		try {
			displayGL = new RendererGL();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}
	
	public class MouseOverListener implements MouseMotionListener {
		public MouseOverListener() {}

		@Override
		public void mouseDragged(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseMoved(MouseEvent e) {
			String key = e.getPoint().x * GameLogic.SCALING + "," + e.getPoint().y * GameLogic.SCALING;
			txtDebug.setText(key + " --- " + gamePanel.landscapeBuffer.debugData.get(key));
		}

	}

	public class MouseZoomListener implements MouseWheelListener {

		@Override
		public void mouseWheelMoved(MouseWheelEvent e) {
			int notches = e.getWheelRotation();
			System.out.println(notches + "   " + GameLogic.SCALING);
			if (notches < 0) {
				GameLogic.SCALING /= 2.0;
			} else {
				GameLogic.SCALING *= 2.0;
			}
			gamePanel.repaint();
		}
	}
}
